/**
 * 
 */
package com.kingcore.director.setting.ui;

import java.awt.Color;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import wzw.beans.DbBean;

import com.kingcore.director.DirectorConstants;
import com.kingcore.framework.context.ApplicationContext;

/**
 * @author WZWEN
 *
 */
public class FileCopyPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JLabel jLabel = null;
	private JLabel jLabel1 = null;
	private JTextField fileExtends = null;
	private JTextField folders = null;
	private JLabel jLabel2 = null;
	private JLabel jLabel3 = null;
	private JLabel jLabel4 = null;

	/**
	 * This method initializes fileExtends	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getFileExtends() {
		if (fileExtends == null) {
			fileExtends = new JTextField();
			fileExtends.setBounds(new java.awt.Rectangle(177,41,251,28));
			
		}
		return fileExtends;
	}

	/**
	 * This method initializes folders	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getFolders() {
		if (folders == null) {
			folders = new JTextField();
			folders.setBounds(new java.awt.Rectangle(177,105,251,28));
		}
		return folders;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

	/**
	 * This is the default constructor
	 */
	public FileCopyPanel() {
		super();
		initialize();
		
		try {
			this.fetchData();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		jLabel4 = new JLabel();
		jLabel4.setBounds(new java.awt.Rectangle(23,161,411,18));
		jLabel4.setText("默认不复制的文件夹:tmp,temp,temporary,tmp_*,temp_*(不分大小写).");
		jLabel4.setForeground(Color.red);
		jLabel3 = new JLabel();
		jLabel3.setForeground( Color.red);
		jLabel3.setBounds(new java.awt.Rectangle(23,136,411,24));
		jLabel3.setText("多个文件夹名用逗号分割，区分大小写，如： CVS,classes");
		jLabel2 = new JLabel();
		jLabel2.setForeground( Color.red);
		jLabel2.setBounds(new java.awt.Rectangle(23,71,388,24));
		jLabel2.setText("多个扩展名用逗号分割，不区分大小写，如： .class,.temp,.tmp");
		jLabel1 = new JLabel();
		jLabel1.setBounds(new java.awt.Rectangle(18,108,171,27));
		jLabel1.setText("不需要复制的文件夹名：");
		jLabel = new JLabel();
		jLabel.setBounds(new java.awt.Rectangle(18,40,169,28));
		jLabel.setText("不需要复制的文件扩展名：");
		this.setSize(448, 307);
		this.setLayout(null);
		this.add(jLabel, null);
		this.add(jLabel1, null);
		this.add(getFileExtends(), null);
		this.add(getFolders(), null);
		this.add(jLabel2, null);
		this.add(jLabel3, null);
		this.add(jLabel4, null);
	}

	public boolean saveData() throws SQLException {
		DbBean dbBean = new DbBean(DbBean.DefaultPoolName);
		try {
			List<String> l_sql=new ArrayList<String>();
			
			l_sql.add( "UPDATE "+DirectorConstants.SYS_Setting+" SET VAL='"+ fileExtends.getText() + "' " +
					"Where KEY='"+DirectorConstants.File_Copy_Not_Include_File_Extends+"'" );
			l_sql.add( "UPDATE "+DirectorConstants.SYS_Setting+" SET VAL='"+ folders.getText() + "' " +
					"Where KEY='"+DirectorConstants.File_Copy_Not_Include_Folders+"'" );
			
			dbBean.executeBatch(l_sql);
			
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		}
		
	}

	public boolean fetchData() throws SQLException {

		String sql="";
		Connection conn=null;
		Statement stmt=null;
		ResultSet rs=null;
		try {
			conn= ApplicationContext.getInstance().getDataSourceManager().getConnection();
			stmt=conn.createStatement();
			sql =  "SELECT VAL FROM "+DirectorConstants.SYS_Setting +
					" Where KEY='"+DirectorConstants.File_Copy_Not_Include_File_Extends+"'" ;
			rs=stmt.executeQuery(sql);
			if (rs.next()) {
				fileExtends.setText( rs.getString("VAL") );
			}else{
				stmt.executeUpdate("Insert Into "+DirectorConstants.SYS_Setting+"(Key,Val) " +
								" values('"+DirectorConstants.File_Copy_Not_Include_File_Extends+"','.temp')");
				conn.commit();
				fileExtends.setText( ".temp" );
			}
			
			sql =  "SELECT VAL FROM "+DirectorConstants.SYS_Setting +
					" Where KEY='"+DirectorConstants.File_Copy_Not_Include_Folders+"'" ;
			rs=stmt.executeQuery(sql);
			if (rs.next()) {
				folders.setText( rs.getString("VAL"));
			}else{
				stmt.executeUpdate("Insert Into "+DirectorConstants.SYS_Setting+"(Key,Val) " +
						" values('"+DirectorConstants.File_Copy_Not_Include_Folders+"','CVS')");
				conn.commit();
				folders.setText( "CVS");
			}
				
			
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(this,e1.getMessage(),"查询失败",JOptionPane.ERROR_MESSAGE);
			e1.printStackTrace();						
		} finally{
			//释放资源
			try {
				if(rs!=null){
					rs.close();
				}
				if(stmt!=null){
					stmt.close();
				}
				if(conn!=null){
					conn.close();
				}
			} catch (SQLException e1) {
				// e1.printStackTrace();
			}
		}
		return true;
	}
	
}  //  @jve:decl-index=0:visual-constraint="10,10"
